(function () {
    'use strict';

    class RoomData {
        constructor() {
            this.subJson = null;
        }
    }

    class TipsManager extends Laya.Script {
        static showTip(msg, owner) {
            let label;
            if (this.tips.length <= 0) {
                label = new Laya.Label("");
                label.fontSize = 45;
                label.color = "#f4f4f3";
                label.bold = true;
                Laya.stage.addChild(label);
                label.visible = false;
                this.tips.push(label);
            }
            label = this.tips.shift();
            label.text = msg;
            label.x = (Laya.stage.width - label.width) / 2;
            label.y = (Laya.stage.height - label.height) / 2;
            let y = label.y - 200;
            label.visible = true;
            label.alpha = 1;
            label.zOrder = 5;
            Laya.Tween.to(label, { y: y, alpha: 0 }, 1 * 1000, Laya.Ease.linearNone, Laya.Handler.create(this, () => {
                label.visible = false;
                this.tips.push(label);
            }));
        }
    }
    TipsManager.tips = [];

    class urlLoader extends Laya.Script {
        onAwake() {
            this.jsonPath = "SubUrlList.json";
            Laya.loader.load(this.jsonPath, Laya.Handler.create(this, this.onLoaded), null, Laya.Loader.JSON);
        }
        onLoaded() {
            urlLoader.urlDic = Laya.loader.getRes(this.jsonPath);
            console.log(urlLoader.urlDic);
        }
    }
    urlLoader.urlDic = null;

    class GameManager extends Laya.Script {
        constructor() {
            super(...arguments);
            this.panelList = [];
            this.Instance = null;
        }
        onAwake() {
            this.Instance = this;
            this.panelList["MainPanel"] = this.owner.parent.getChildByName("MainPanel");
            this.panelList["MainPanel"].visible = false;
            this.panelList["BuildingPanel"] = this.owner.parent.getChildByName("BuildingPanel");
            this.panelList["BuildingPanel"].visible = false;
            this.panelList["FloorPanel"] = this.owner.parent.getChildByName("FloorPanel");
            this.panelList["FloorPanel"].visible = false;
            this.panelList["RoomPanel"] = this.owner.parent.getChildByName("RoomPanel");
            this.panelList["RoomPanel"].visible = false;
            this.roomPanel = this.panelList["RoomPanel"];
            Laya.stage.on("onBtn", this, this.OnBtnClick);
            Laya.stage.on("onReturnFloor", this, this.onReturnFloor);
            Laya.stage.on("OnClickReturnBuild", this, this.OnClickReturnBuild);
        }
        OnClickReturnBuild(arg0, arg1, OnClickReturnBuild) {
            this.ChangePanel("BuildingPanel");
        }
        onStart() {
            this.ChangePanel("MainPanel");
        }
        onReturnFloor() {
            this.ChangePanel("FloorPanel");
        }
        ChangePanel(panelName) {
            if (this.curPanel != null) {
                this.curPanel.visible = false;
            }
            this.panelList[panelName].visible = true;
            this.curPanel = this.panelList[panelName];
        }
        OnBtnClick(btnName) {
            if (btnName == "StartBtn") {
                this.ChangePanel("BuildingPanel");
            }
            else if (btnName.includes("build")) {
                this.ChangePanel("FloorPanel");
                var buildNum = Number.parseInt(btnName.replace("build_", ""));
                GameManager.curBuilding = buildNum;
            }
            else if (btnName.includes("floor")) {
                var floorNum = Number.parseInt(btnName.replace("floor_", ""));
                GameManager.curFloor = floorNum;
                var buildDic = urlLoader.urlDic[GameManager.curBuilding.toString()];
                if (buildDic == null) {
                    TipsManager.showTip("暂无链接", Laya.stage);
                }
                else {
                    var floorDic = buildDic[GameManager.curFloor.toString()];
                    if (floorDic == null) {
                        TipsManager.showTip("暂无链接", Laya.stage);
                    }
                    else {
                        var panel = this.roomPanel.getComponent(RoomPanel);
                        panel.Refresh(floorDic);
                        this.ChangePanel("RoomPanel");
                    }
                }
            }
        }
    }

    var Box = Laya.Box;
    class Item extends Box {
        constructor() {
            super();
            this.size(Item.WID, Item.HEI);
            this.img = new Laya.Image();
            this.img.width = Item.WID;
            this.img.height = Item.HEI - 80;
            this.bg = new Laya.Image();
            this.bg.width = Item.WID;
            this.bg.height = Item.HEI;
            this.myName = new Laya.Text();
            this.myName.font = "Arial";
            this.myName.fontSize = 40;
            this.myName.bold = true;
            this.myName.valign = "middle";
            this.myName.x = 23;
            this.myName.y = 235;
            this.myName.width = 403;
            this.myName.height = 84;
            this.addChild(this.bg);
            this.addChild(this.img);
            this.addChild(this.myName);
            this.bg.on(Laya.Event.CLICK, this, this.onClick);
        }
        setImg(src) {
            this.bg.skin = "whitebg.jpg";
            var nameStr = src.name;
            this.url = src.url;
            this.subJson = src.subJson;
            this.myName.text = GameManager.curBuilding.toString() + "座" + nameStr;
            var skinName = "RoomImg/" + GameManager.curBuilding.toString() + "/" + nameStr + ".jpg";
            this.img.skin = skinName;
        }
        onClick() {
            if (this.subJson != null) {
                Laya.stage.event("OnClickSub", this.subJson);
            }
            else {
                Laya.stage.event("OnClickUrl", this.url);
            }
        }
    }
    Item.WID = 431;
    Item.HEI = 325;

    var Handler = Laya.Handler;
    class RoomPanel extends Laya.Script {
        constructor() {
            super(...arguments);
            this.myBtn = null;
            this.btnList = new Array();
        }
        onAwake() {
            this.myList = this.owner.getChildByName("List");
            this.myList.itemRender = Item;
            this.myList.selectEnable = true;
            this.myList.selectHandler = new Handler(this, this.onSelect);
            this.myList.renderHandler = new Handler(this, this.updateItem);
            this.myList.vScrollBarSkin = "";
            Laya.stage.on("OnClickSub", this, this.onClickSubJson);
            Laya.stage.on("OnClickUrl", this, this.onClickUrl);
            var returnBtn = this.owner.getChildByName("returnBtn");
            returnBtn.on(Laya.Event.CLICK, this, this.onClickReturn);
        }
        onClickReturn() {
            if (this.isSub) {
                this.Refresh(this.lastJson);
            }
            else {
                Laya.stage.event("onReturnFloor");
            }
        }
        onSelect(index) {
        }
        updateItem(cell, index) {
            cell.setImg(cell.dataSource);
        }
        onClickSubJson(subJson) {
            this.Refresh(subJson, true);
        }
        onClickUrl(url) {
            window.location.href = url;
        }
        Refresh(urlDic, isSub = false) {
            if (!isSub) {
                this.lastJson = urlDic;
            }
            this.isSub = isSub;
            var urlCount = 0;
            var data = [];
            for (var key in urlDic) {
                var room = new RoomData();
                room.name = key;
                if (key.includes("--")) {
                    room.subJson = urlDic[key];
                }
                room.url = urlDic[key];
                data.push(room);
            }
            this.myList.array = data;
            this.myList.refresh();
        }
    }

    class Btn extends Laya.Script {
        constructor() {
            super(...arguments);
            this.MyName = "";
            this.myBtn = null;
        }
        onAwake() {
            this.MyName = this.owner.name;
            this.owner.on(Laya.Event.CLICK, this, this.OnClick);
        }
        OnClick() {
            Laya.stage.event("onBtn", this.MyName);
        }
    }

    class ReturnBuildBtn extends Laya.Script {
        onAwake() {
            this.myBtn = this.owner;
            this.myBtn.on(Laya.Event.CLICK, this, this.onClick);
        }
        onClick() {
            Laya.stage.event("OnClickReturnBuild");
        }
    }

    class GameConfig {
        constructor() {
        }
        static init() {
            var reg = Laya.ClassUtils.regClass;
            reg("Main/RoomPanel.ts", RoomPanel);
            reg("Main/Btn.ts", Btn);
            reg("Main/ReturnBuildBtn.ts", ReturnBuildBtn);
            reg("Main/GameManager.ts", GameManager);
            reg("Main/urlLoader.ts", urlLoader);
        }
    }
    GameConfig.width = 960;
    GameConfig.height = 1920;
    GameConfig.scaleMode = "showall";
    GameConfig.screenMode = "vertical";
    GameConfig.alignV = "middle";
    GameConfig.alignH = "center";
    GameConfig.startScene = "Main.scene";
    GameConfig.sceneRoot = "";
    GameConfig.debug = false;
    GameConfig.stat = false;
    GameConfig.physicsDebug = false;
    GameConfig.exportSceneToJson = true;
    GameConfig.init();

    class Main {
        constructor() {
            if (window["Laya3D"])
                Laya3D.init(GameConfig.width, GameConfig.height);
            else
                Laya.init(GameConfig.width, GameConfig.height, Laya["WebGL"]);
            Laya["Physics"] && Laya["Physics"].enable();
            Laya["DebugPanel"] && Laya["DebugPanel"].enable();
            Laya.stage.scaleMode = GameConfig.scaleMode;
            Laya.stage.screenMode = GameConfig.screenMode;
            Laya.stage.alignV = GameConfig.alignV;
            Laya.stage.alignH = GameConfig.alignH;
            Laya.URL.exportSceneToJson = GameConfig.exportSceneToJson;
            if (GameConfig.debug || Laya.Utils.getQueryString("debug") == "true")
                Laya.enableDebugPanel();
            if (GameConfig.physicsDebug && Laya["PhysicsDebugDraw"])
                Laya["PhysicsDebugDraw"].enable();
            if (GameConfig.stat)
                Laya.Stat.show();
            Laya.alertGlobalError(true);
            Laya.ResourceVersion.enable("version.json", Laya.Handler.create(this, this.onVersionLoaded), Laya.ResourceVersion.FILENAME_VERSION);
        }
        onVersionLoaded() {
            Laya.AtlasInfoManager.enable("fileconfig.json", Laya.Handler.create(this, this.onConfigLoaded));
        }
        onConfigLoaded() {
            GameConfig.startScene && Laya.Scene.open(GameConfig.startScene);
        }
    }
    new Main();

}());
